<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */

    /*********************************************************
    *            --== InArch ==--
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @since 04.05.2005
    * @copyright essentialmind.com 2005
    *
    *********************************************************/
    
    /***
    * Class mailGateway
    ****
    * Sends an e-mail to the defined address. This class may be useful
    * when willing to decode all the outgoing e-mail or in order to
    * perform some mass function when e-mail is a question of thought.
    ****
    * @package usefulStuff
    * @author A Kulikov <a.kulikov@gmail.com>
    * @version n/a
    * @since 04.05.2005
    * @copyright essentialmind.com 2005
    ***/
    class mailGateway{
        
        protected $attachments = array();
        
        /**
         * Enter description here...
         *
         */
        public function __construct(){
            //do nothing
        }
        
        
        /**
         * Sends an e-mail to the address specifid in the first parameter
         *
         * @param string $to        - to whom the e-mail will be sent
         * @param string $subject   - the subject of the e-mail
         * @param string $text      - the text of the e-mail
         * @param string $from      - from whom did the e-mail originate?
         * @return bool
         */
        public function send($to,$subject,$text,$from=null,$replyto=null){
            //prepare empty var
            $headers = null;
            $eol     = "\n";
            $boundary= "ina-vh-88772629";
            
            //take the default BOT address
            if(!$from){
                $from = $GLOBALS['_CONFIG']['bot'];
            }
            
            if(isset($from)){
                $headers .= 'From: '.$from.$eol;
                $headers .= 'Reply-To: '.($replyto?$replyto:$from).$eol;
                $headers .= 'Return-Path: '.$from.$eol;    // these two to set reply address
                $headers .= "X-Mailer: PHP v".phpversion().$eol;          // These two to help avoid spam-filters
            }

            if(!empty($this->attachments)){
                $headers .= 'MIME-Version: 1.0'.$eol;
                $headers .= "Content-Type: multipart/mixed; boundary=\"".$boundary."\";".$eol;
                
                $message  = '--'.$boundary.$eol;
                $message .= 'Content-Type: text/plain; charset="UTF-8"'.$eol;
                $message .= 'Content-Transfer-Encoding: 7bit'.$eol.$eol;
                $message .= $text.$eol.$eol;
                
                foreach($this->attachments as $attachment){
                    $message .= '--'.$boundary.$eol;
                    $message .= 'Content-Transfer-Encoding: base64'.$eol;
                    $message .= 'Content-Type: '.$attachment['type'].'; name="'.$attachment['name'].'"'.$eol;
                    $message .= 'Content-Disposition: attachment; filename="'.$attachment['name'].'"'.$eol.$eol;
                    $message .= chunk_split(base64_encode($attachment['data'])).$eol.$eol;
                }
                
                $message .= '--'.$boundary.'--'.$eol.$eol;
            }else{
                $headers .= "Content-Type: text/plain; charset=UTF-8".$eol;
                $message = $text;
            }
 		
            
            //add some default headers
            //$headers[] = 'Content-type: text/plain; charset=utf-8';
            //$headers[] = 'X-Mailer: Generated By INA';
            
            //$fp=fopen('cache/mes.txt','wb'); 
            //fwrite($fp,$to."\n".$headers."\n".$message); 
            //fclose($fp); 

            //try to send the e-mail
            return @mail($to,"=?UTF-8?B?" . base64_encode($subject) . "?=",$message,$headers);
        } 


		/**
         * Sends an e-mail in html multipart format to the address specifid
 		 * in the first parameter. if $html param is null it sends a
		 * multipart email with only plain/text 
         *
         * @param string $to        - to whom the e-mail will be sent
         * @param string $subject   - the subject of the e-mail
         * @param string $text      - the text of the e-mail
         * @param string $html      - the html of the e-mail
         * @param string $from      - from whom did the e-mail originate?
         * @return bool
         */
        public function send_html($to,$subject,$text,$html=null,$from=null,$replyto=null){
            //prepare empty var
            $headers = null;
            $eol     = "\n";
            $boundary= "ina-vh-88772629";
            
            if(isset($from)){
                $headers .= 'From: '.$from.$eol;
                $headers .= 'Reply-To: '.($replyto?$replyto:$from).$eol;
                $headers .= 'Return-Path: '.$from.$eol;    // these two to set reply address
                $headers .= "X-Mailer: PHP v".phpversion().$eol;          // These two to help avoid spam-filters
            }
            
            
            // add text version
            $headers .= 'MIME-Version: 1.0'.$eol;
            $headers .= "Content-Type: multipart/mixed; boundary=\"".$boundary."\";".$eol;
            
            $message  = '--'.$boundary.$eol;
            $message .= 'Content-Type: text/plain; charset="UTF-8"'.$eol;
            $message .= 'Content-Transfer-Encoding: 7bit'.$eol.$eol;
            $message .= $text.$eol.$eol;

			$message .= '--'.$boundary.'--'.$eol.$eol;
        
			// add html version
			if($html!=null) {
				$headers .= 'MIME-Version: 1.0'.$eol;
	            $headers .= "Content-Type: multipart/mixed; boundary=\"".$boundary."\";".$eol;

	            $message  = '--'.$boundary.$eol;
	            $message .= 'Content-Type: text/html; charset="UTF-8"'.$eol;
	            $message .= 'Content-Transfer-Encoding: 7bit'.$eol.$eol;
	            $message .= $html.$eol.$eol;
	
				$message .= '--'.$boundary.'--'.$eol.$eol;
				
			}
			
			//in case the from address is set, then
            //add this as an e-mail header                                    
    		if(!empty($this->attachments)){
                foreach($this->attachments as $attachment){
                    $message .= '--'.$boundary.$eol;
                    $message .= 'Content-Transfer-Encoding: base64'.$eol;
                    $message .= 'Content-Type: '.$attachment['type'].'; name="'.$attachment['name'].'"'.$eol;
                    $message .= 'Content-Disposition: attachment; filename="'.$attachment['name'].'"'.$eol.$eol;
                    $message .= chunk_split(base64_encode($attachment['data'])).$eol.$eol;
                }
                $message .= '--'.$boundary.'--'.$eol.$eol;
            }
            
            //add some default headers
            //$headers[] = 'Content-type: text/plain; charset=utf-8';
            //$headers[] = 'X-Mailer: Generated By INA';
            
            //$fp=fopen('cache/mes.txt','wb'); 
            //fwrite($fp,$to."\n".$headers."\n".$message); 
            //fclose($fp); 

            //try to send the e-mail
            return @mail($to,"=?UTF-8?B?" . base64_encode($subject) . "?=",$message,$headers);
        } 
        
        
        
        public function attach($name, $data, $type=''){
            $item = array();
            $item['name'] = $name;
            $item['type'] = $type;
            $item['data'] = $data;
            
            $this->attachments[] = $item;
        }
    }
?>